import enquireJs from 'enquire.js'

const _toString = Object.prototype.toString

export function isDef(v) {
  return v !== undefined && v !== null
}

/**
 * Remove an item from an array.
 */
export function remove(arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item)
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

export function isRegExp(v) {
  return _toString.call(v) === '[object RegExp]'
}

export function enquireScreen(call) {
  const handler = {
    match: function () {
      call && call(true)
    },
    unmatch: function () {
      call && call(false)
    }
  }
  enquireJs.register('only screen and (max-width: 767.99px)', handler)
}

export function transListToTree(list, pid) {
  const result = [];
  for (let i = 0; i < list.length; i++) {
    const node = list[i];
    if (node.parentId == pid) {
      const children = transListToTree(list, node.id);
      const newNode = children && children.length ? { ...node, children } : node;
      result.push(newNode);
    }
  }
  return result;
}